package com.asion.gankapp.view.fragments;

import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.asion.gankapp.R;
import com.asion.gankapp.adapters.BaseRecyclerAdapter;
import com.asion.gankapp.utils.refresh.CircleRefreshLayout;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;

/**
 * Created by asion on 16-10-6.
 */

public class DownLoadFragment extends Fragment implements CircleRefreshLayout.OnCircleRefreshListener {

    private RecyclerView recycler;

    public static DownLoadFragment newInstance(){
        return new DownLoadFragment();
    }
    private CircleRefreshLayout refresh;
    private File file = new File(Environment.getExternalStorageDirectory() + File.separator + "Gank_Pictures" );

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 100:
                    refresh.finishRefreshing();
                    break;
            }
        }
    };
    private View container;
    private BaseRecyclerAdapter baseRecyclerAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_base_recycler,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        refresh = ((CircleRefreshLayout) view.findViewById(R.id.refresh_layout));
        container = view.findViewById(R.id.base_recycler_container);
        recycler = ((RecyclerView) view.findViewById(R.id.base_recycler));

        recycler.setItemAnimator(new DefaultItemAnimator());
        recycler.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));

        baseRecyclerAdapter = new BaseRecyclerAdapter(getContext());
        baseRecyclerAdapter.setType(3);
        recycler.setAdapter(baseRecyclerAdapter);

        searchFile();

        baseRecyclerAdapter.setOnDeleteClickListener((v,position,filename)->{
            FilenameFilter filenameFilter = (dir, name) -> name.contains(filename);
            if (file.isDirectory()) {
                File[] files = file.listFiles(filenameFilter);
                for (File file1 : files) {
                    file1.delete();
                }
                baseRecyclerAdapter.remove(position);
            }
        });

        refresh.setOnRefreshListener(this);
    }

    @Override
    public void completeRefresh() {
        Snackbar.make(container, "刷新完成", Snackbar.LENGTH_SHORT).show();
    }

    private void searchFile() {
        FilenameFilter filenameFilter = (dir, name) -> name.contains(".jpg");
        if (file.isDirectory()) {
            File[] files = file.listFiles(filenameFilter);
            baseRecyclerAdapter.addAll(Arrays.asList(files));
        }
    }

    @Override
    public void refreshing() {
        baseRecyclerAdapter.removeAll();
        FilenameFilter filenameFilter = (dir, name) -> name.contains(".jpg");
        if (file.isDirectory()) {
            File[] files = file.listFiles(filenameFilter);
            baseRecyclerAdapter.addAll(Arrays.asList(files));
        }
        handler.sendEmptyMessageDelayed(100,3000);
    }
}
